package com.ym.jiujiangeyou.database;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ym.jiujiangeyou.R;
import com.ym.jiujiangeyou.entity.Guide;
import com.ym.jiujiangeyou.entity.Scenery;
import com.ym.jiujiangeyou.util.FileManager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 * @Description: scenery 数据库操作类
 */
public class SceneryService {

	DataBaseHelper helper;
	SQLiteDatabase db;
	String[] columns = new String[] { "sname", "sid", "address",
			"introduction", "ticket", "type", "imageurl", "longitude",
			"latitude", "openTime", "phoneNumber", "traffic", "prompt",
			"gticket"};
	Context context;

	public SceneryService(Context context) {
		helper = new DataBaseHelper(context);
		this.context = context;
	}

	/**
	 * @param scenery
	 * @param guide
	 * @retur long
	 * @Description: 保存scenery guide 信息
	 */
	public long insetSceneryToBase(Scenery scenery, Guide guide) {
		long i = 0;
		db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("sname", scenery.getName());
		values.put("sid", scenery.getId());
		values.put("address", scenery.getAddress());
		values.put("introduction", scenery.getIntroduction());
		values.put("ticket", scenery.getTicket());
		values.put("type", scenery.getType());
		values.put("imageurl", scenery.getImageurl());
		values.put("longitude", scenery.getLongitude());
		values.put("latitude", scenery.getLatitude());
		values.put("openTime", guide.getOpenTime());
		values.put("phoneNumber", guide.getPhoneNumber());
		values.put("traffic", guide.getTraffic());
		values.put("prompt", guide.getPrompt());
		values.put("gticket", guide.getTicket());

		/*Bitmap bitmap = getImg(scenery.getImageurl());
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
		values.put("simg", stream.toByteArray());*/

		i = db.insert("scenery", null, values);
		db.close();
		return i;

	}

	/**
	 * @param sid
	 * @return
	 * @retur boolean
	 * @Description: 根据name查询
	 */
	public boolean queryByName(String sname) {
		boolean flag = false;
		db = helper.getReadableDatabase();
		Cursor cursor = db.query("scenery", columns, "sname = ?",
				new String[] { sname }, null, null, null);
		if (cursor != null && cursor.moveToNext()) {
			flag = true;
		}
		db.close();
		return flag;
	}

	/**
	 * @param sid
	 * @return
	 * @retur Map<String,Object>
	 * @Description: 根据sid查询数据信息
	 */
	public Map<String, Object> queryAll(int sid) {
		db = helper.getReadableDatabase();
		Map<String, Object> map = new HashMap<String, Object>();
		Scenery scenery = null;
		Guide guide = null;
		Cursor cursor = db.query("scenery", columns, "sid = ?",
				new String[] { String.valueOf(sid) }, null, null, null);
		if (cursor != null) {
			while (cursor.moveToNext()) {
				scenery = new Scenery();
				guide = new Guide();
				scenery.setId(cursor.getInt(cursor.getColumnIndex("sid")));
				scenery.setName(cursor.getString(cursor.getColumnIndex("sname")));
				scenery.setAddress(cursor.getString(cursor
						.getColumnIndex("address")));
				scenery.setIntroduction(cursor.getString(cursor
						.getColumnIndex("introduction")));
				scenery.setTicket(cursor.getString(cursor
						.getColumnIndex("ticket")));
				scenery.setType(cursor.getString(cursor.getColumnIndex("type")));
				scenery.setImageurl(cursor.getString(cursor
						.getColumnIndex("imageurl")));
				scenery.setLatitude(cursor.getDouble(cursor
						.getColumnIndex("latitude")));
				scenery.setLongitude(cursor.getDouble(cursor
						.getColumnIndex("longitude")));
				guide.setId(cursor.getInt(cursor.getColumnIndex("sid")));
				guide.setName(cursor.getString(cursor.getColumnIndex("sname")));
				guide.setOpenTime(cursor.getString(cursor
						.getColumnIndex("openTime")));
				guide.setPhoneNumber(cursor.getString(cursor
						.getColumnIndex("phoneNumber")));
				guide.setTraffic(cursor.getString(cursor
						.getColumnIndex("traffic")));
				guide.setPrompt(cursor.getString(cursor
						.getColumnIndex("prompt")));
				guide.setTicket(cursor.getString(cursor
						.getColumnIndex("gticket")));
				/*byte[] bt = cursor.getBlob(cursor.getColumnIndex("simg"));
				Bitmap bitmap = BitmapFactory.decodeByteArray(bt, 0, bt.length);
				scenery.setSimg(bitmap);*/
				map.put("scenery", scenery);
				map.put("guide", guide);

			}
		}
		db.close();
		return map;
	}

	/**
	 * @return
	 * @retur Map<String,Object>
	 * @Description: 检索所有数据信息
	 */
	public Map<String, Object> query() {
		db = helper.getReadableDatabase();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Scenery> scenerys = new ArrayList<Scenery>();
		List<Guide> guides = new ArrayList<Guide>();
		Scenery scenery = null;
		Guide guide = null;
		Cursor cursor = db.query("scenery", columns, null, null, null, null,
				null);
		if (cursor != null) {
			while (cursor.moveToNext()) {
				scenery = new Scenery();
				guide = new Guide();
				scenery.setId(cursor.getInt(cursor.getColumnIndex("sid")));
				scenery.setName(cursor.getString(cursor.getColumnIndex("sname")));
				scenery.setAddress(cursor.getString(cursor
						.getColumnIndex("address")));
				scenery.setIntroduction(cursor.getString(cursor
						.getColumnIndex("introduction")));
				scenery.setTicket(cursor.getString(cursor
						.getColumnIndex("ticket")));
				scenery.setType(cursor.getString(cursor.getColumnIndex("type")));
				scenery.setImageurl(cursor.getString(cursor
						.getColumnIndex("imageurl")));
				scenery.setLatitude(cursor.getDouble(cursor
						.getColumnIndex("latitude")));
				scenery.setLongitude(cursor.getDouble(cursor
						.getColumnIndex("longitude")));
				guide.setId(cursor.getInt(cursor.getColumnIndex("sid")));
				guide.setName(cursor.getString(cursor.getColumnIndex("sname")));
				guide.setOpenTime(cursor.getString(cursor
						.getColumnIndex("openTime")));
				guide.setPhoneNumber(cursor.getString(cursor
						.getColumnIndex("phoneNumber")));
				guide.setTraffic(cursor.getString(cursor
						.getColumnIndex("traffic")));
				guide.setPrompt(cursor.getString(cursor
						.getColumnIndex("prompt")));
				guide.setTicket(cursor.getString(cursor
						.getColumnIndex("gticket")));
				/*byte[] bt = cursor.getBlob(cursor.getColumnIndex("simg"));
				Bitmap bitmap = BitmapFactory.decodeByteArray(bt, 0, bt.length);
				scenery.setSimg(bitmap);*/
				scenerys.add(scenery);
				guides.add(guide);
			}
			map.put("scenery", scenerys);
			map.put("guide", guides);
		}
		db.close();
		return map;
	}
	
	/** 
	* @param name  
	* @retur  void 
	* @Description: 根据name删除 
	*/
	public void delByName(String name){
		db = helper.getReadableDatabase();
		String sql = "delete from scenery where sname = ?" ;
		db.execSQL(sql, new Object[]{name}) ;
		db.close() ;
	}

	/**
	 * @param url
	 * @return
	 * @retur Bitmap
	 * @Description: 获取图片
	 */
	public Bitmap getImg(String url) {
		Bitmap bitmap = FileManager.getBitmap(url);
		if (bitmap == null) {
			bitmap = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.empty_photo);
		}
		return bitmap;

	}

}
